import * as types from './mutation-types'
import { allApi } from '../api/api'
import Storage from '../utils/storage'

export const showLoading = ({ commit, state }, title) => commit(types.SHOW_LOADING, title)
export const hideLoading = ({ commit, state }) => {
  // setTimeout(() => {
    commit(types.HIDE_LOADING)
  // },0)
}

export const setMsgTip = ({ commit, state },msgObj) => {
    let timer = setTimeout(() => {
        commit(types.HIDE_MSGTIP)
    },3000)
    msgObj.timer = timer
    commit(types.SET_MSGTIP, msgObj)
}
export const setListInfo = ({ commit, state },listObj) => {
    commit(types.SET_LISTINFO, listObj)
}
export const setMsgTipStyle = ({ commit, state },styleObj) => {
    commit(types.SET_MSGTIPSTYLE, styleObj)
}

export const setMenuList = ({ commit, state },arr) => {
    
    if(!arr){
        arr = Storage.get(`51SjdAdmin_menuArr`) || []
    }
    let partantArr=[],childArr=[];
    _.each(arr,(value,index)=>{
        //console.log(value)
        
        if(value.type==0){
            partantArr.push(value)
        }else{
            childArr.push(value)
        }
    })

for(let i = 0;i<partantArr.length;i++){
        var arrdd=[]
    for(let j = 0;j<childArr.length;j++){
            if(childArr[j].parentId==partantArr[i].id){
                arrdd.push(childArr[j])
            }
    }
    partantArr[i].subitem=arrdd
}
//console.log(partantArr)
var newarrs = []
_.each(partantArr,(value3,index3)=>{
    //仪表盘
    if(value3.subitem!=""){
        if(value3.code=="dashboard"){
            let subRouter=[];
            for(var i = 0;i<value3.subitem.length;i++){
               if( value3.subitem[i].id==27){
                    subRouter.push({
                        name:'dash-board',
                        text:value3.subitem[i].info
                    })
               }
            }
            let parr=[{
                text:value3.info,
                router:'dash-board',
                id:1,
                opened:false,
                subRouters:subRouter
            }];
            newarrs.push(parr)
            return
            
        }
        //认证申请管理
        if(value3.code=="auth"){
            let subRouter=[];
            for(var i = 0;i<value3.subitem.length;i++){
               if( value3.subitem[i].id==14){
                    subRouter.push({
                        name:'auth-apply-history',
                        text:value3.subitem[i].info
                    })
               }
            }
            let parr=[{
                text:value3.info,
                router:'auth-apply',
                id:3,
                opened:false,
                subRouters:subRouter
            }];
            newarrs.push(parr)
           return
            
        }
        //运营管理
        if(value3.code=="operation"){
            let subRouter=[];
            for(var i = 0;i<value3.subitem.length;i++){
                if( value3.subitem[i].id==25){
                    subRouter.push({
                        name:'operate-assign-records',
                        text:value3.subitem[i].info
                    })
               }
               if( value3.subitem[i].id==26){
                    subRouter.push({
                        name:'operate-assign-records-loan',
                        text:value3.subitem[i].info
                    })
               }
            }

            let parr=[{
                text:value3.info,
                router:'operate',
                id:2,
                opened:false,
                subRouters:subRouter
            }];
            newarrs.push(parr)
            return
            
        }
        //客户管理
        if(value3.code=="cust"){
            let subRouter=[];
            for(var i = 0;i<value3.subitem.length;i++){
                if( value3.subitem[i].id==32){
                    subRouter.push({
                        name:'staff-management',
                        id:15,
                        text:value3.subitem[i].info
                    })
               }
              
            }
             subRouter=subRouter.reverse()
             let parr=[{
                text:value3.info,
                router:'staff',
                id:4,
                opened:false,
                subRouters:subRouter
            }];
            newarrs.push(parr)
            return
        }
        /*if(value3.code=="cust"){
            let subRouter=[];
            for(var i = 0;i<value3.subitem.length;i++){
                if( value3.subitem[i].id==32){
                    subRouter.push({
                        name:'staff-management',
                        id:15,
                        text:value3.subitem[i].info
                    })
               }
              
            }
             //console.log(subRouter)
            subRouter=subRouter.reverse()
            let parr=[{
                text:value3.info,
                router:'staff',
                id:4,
                opened:false,
                subRouters:subRouter
            }];
            newarrs.push(parr)
            return
            
        }*/
        //借款申请管理
        if(value3.code=="loan"){
            let subRouter=[];
            for(var i = 0;i<value3.subitem.length;i++){
                if( value3.subitem[i].id==15){
                    subRouter.push({
                        name:'loan-apply-wait',
                        id:15,
                        text:value3.subitem[i].info
                    })
               }
               if( value3.subitem[i].id==16){
                    subRouter.push({
                        name:'loan-apply-history',
                        id:16,
                        text:value3.subitem[i].info
                    })
               }
            }
             //console.log(subRouter)
            subRouter=subRouter.reverse()
            let parr=[{
                text:value3.info,
                router:'loan-apply',
                id:5,
                opened:false,
                subRouters:subRouter
            }];
            newarrs.push(parr)
            return
            
        }
        //财务管理
        if(value3.code=="lend"){
            let subRouter=[];
            for(var i = 0;i<value3.subitem.length;i++){
                if( value3.subitem[i].id==17){
                    subRouter.push({
                        name:'finance-wait',
                        text:value3.subitem[i].info
                    })
               }
               if( value3.subitem[i].id==18){
                    subRouter.push({
                        name:'finance-out',
                        text:value3.subitem[i].info
                    })
               }
               if( value3.subitem[i].id==12){
                    subRouter.push({
                        name:'receipt',
                        text:value3.subitem[i].info
                    })
               }
               if( value3.subitem[i].id==34){
                    subRouter.push({
                        name:'loan_failure',
                        text:value3.subitem[i].info
                    })
               }

               /*if( value3.subitem[i].info=="展期记录"){
                    subRouter.push({
                        name:'finance-extend',
                        text:"展期记录"
                    })
               }*/
            }
            let parr=[{
                text:value3.info,
                router:'finance',
                id:6,
                opened:false,
                subRouters:subRouter
            }];
            newarrs.push(parr)
            return
        }
        //贷后管理
        if(value3.code=="repay"){
            let subRouter=[];
            for(var i = 0;i<value3.subitem.length;i++){
                if( value3.subitem[i].id==21){
                    subRouter.push({
                        name:'after-loan-wait',
                        text:value3.subitem[i].info
                    })
               }
               if( value3.subitem[i].id==20){
                    subRouter.push({
                        name:'after-loan-history',
                        text:value3.subitem[i].info
                    })
               }
               if( value3.subitem[i].id==19){
                    subRouter.push({
                        name:'after-loan-allot-index',
                        text:value3.subitem[i].info,
                        query:{
                         routerType:'all'
                        },
                    })
               }
               if( value3.subitem[i].id==30){
                    subRouter.push({
                        name:'after-loan-return',
                        text:value3.subitem[i].info,
                        /*query:{
                         routerType:'all'
                        },*/
                    })
               }
            }
            let parr=[{
                text:value3.info,
                router:'after-loan',
                id:7,
                opened:false,
                subRouters:subRouter
            }];
            newarrs.push(parr)
            return
        }
        //渠道管理
        if(value3.code=="channel"){
            let subRouter=[];
            for(var i = 0;i<value3.subitem.length;i++){
                if( value3.subitem[i].id==22){
                    subRouter.push({
                        name:'channel-management',
                        text:value3.subitem[i].info
                    })
               }
               if( value3.subitem[i].id==23){
                    subRouter.push({
                        name:'channel-inquiry',
                        text:value3.subitem[i].info
                    })
               }
            }
            let parr=[{
                text:value3.info,
                router:'channel',
                id:8,
                opened:false,
                subRouters:subRouter
            }];
            newarrs.push(parr)
            return
        }
        //统计报表
        if(value3.code=="financial"){
            let subRouter=[];
            for(var i = 0;i<value3.subitem.length;i++){
                if( value3.subitem[i].id==11){
                    subRouter.push({
                        name:'financial',
                        id:1,
                        text:value3.subitem[i].info
                    })
               }
               if( value3.subitem[i].id==29){
                    subRouter.push({
                        name:'channel-statistics',
                        id:2,
                        text:value3.subitem[i].info
                    })
               }
               if( value3.subitem[i].id==28){
                    subRouter.push({
                        name:'overdue-statistics',
                        id:3,
                        text:value3.subitem[i].info
                    })
               }
            }
            let arr=[],valueArr=[],arr1;
             _.each(subRouter,(value,index)=>{
                arr.push(value.id)
            })
             arr1=arr.sort(compare)
            for(var d = 0;d<arr1.length;d++){
                for(var l = 0;l<subRouter.length;l++){
                    if((subRouter[l].id)==arr1[d]){
                        valueArr.push(subRouter[l])
                    }
                }
            }
            let parr=[{
                text:value3.info,
                router:'financial-body',
                id:9,
                opened:false,
                subRouters:valueArr
            }];
            newarrs.push(parr)
            return
        }
        //通用管理
        if(value3.code=="system"){
            let subRouter=[];
            for(var i = 0;i<value3.subitem.length;i++){
                if( value3.subitem[i].id==24){
                    subRouter.push({
                        name:'system-user',
                        text:value3.subitem[i].info
                    })
                }
                if( value3.subitem[i].id==33){
                    subRouter.push({
                        name:'common-config',
                        text:value3.subitem[i].info
                    })
               }
               /*if( value3.subitem[i].info=="风控条件配置"){
                    subRouter.push({
                        name:'system-risk-config',
                        text:"风控条件配置"
                    })
               }*/
            }
            let parr=[{
                text:value3.info,
                router:'system',
                id:10,
                opened:false,
                subRouters:subRouter
            }];
            newarrs.push(parr)
            return
        }
    }
        
})

    let newarr = []
    let keyArr=[];
    let valueArr=[];
    _.each(newarrs,(value,index)=>{
        keyArr.push(value[0].id)
    })
    let sortArr=keyArr.sort(compare);
    for(var d = 0;d<sortArr.length;d++){
        for(var l = 0;l<newarrs.length;l++){
            if((newarrs[l][0].id)==sortArr[d]){
                valueArr.push(newarrs[l])
            }
        }
    }
    function compare(value1, currentValue){
        return value1 - currentValue;
    }
    // _.each(arr,(value,index)=>{
        
    //     if(value == "channel"){
    //         if(localStorage.getItem("roles")=="channel_operator"){
    //                 newarr.push([{
    //                     text:'渠道查询',
    //                     router:'channel-inquiry',
    //                     opened:false,
    //                     subRouters:[/*{
    //                         name:'channel-management',
    //                         text:'渠道商管理'
    //                     },*/{
    //                         name:'channel-inquiry',
    //                         text:'渠道查询'
    //                     }],
    //                 }])
    //         }else{
    //             newarr.push([{
    //             text:'渠道管理',
    //             router:'channel',
    //             opened:false,
    //             subRouters:[{
    //                 name:'channel-management',
    //                 text:'渠道商管理'
    //             },{
    //                 name:'channel-inquiry',
    //                 text:'渠道查询'
    //             }],
    //         }])
    //         }
            
    //     }
    //     if(value == "dashboard"){ //
    //         newarr.push([{
    //             text:'仪表盘',
    //             router:'dash-board',
    //             opened:false,
    //              subRouters:[{
    //                 name:'dash-board',
    //                 text:'仪表盘'
    //             }],
    //         }])
            
           
    //         return
    //     }
    //     if(value == 'financial'){
           
    //          newarr.push([{
    //             text:'统计报表',
    //             router:'financial-body',
    //             opened:false,
    //              subRouters:[{
    //                 name:'financial',
    //                 text:'财务统计报表'
    //             }],
    //         }])
    //         return
    //     }
    //     // if(value == 'receipt'){
    //     //     newarr.push([{
    //     //         text:'应收账款统计',
    //     //         router:'receipt',
    //     //         opened:false,
    //     //          subRouters:[{
    //     //             name:'receipt',
    //     //             text:'应收账款统计'
    //     //         }],
    //     //     }])
    //     //     return
    //     // }
    //   if(value == 'system'){
    //         newarr.push([{
    //             text:'通用管理',
    //             router:'system',
    //             opened:false,
    //             subRouters:[{
    //                 name:'system-user',
    //                 text:'员工账号管理'
    //             },{
    //                 name:'system-product-config',
    //                 text:'产品配置'
    //             },{
    //                 name:'system-risk-config',
    //                 text:'风控条件配置'
    //             }/*,{
    //                 name:'system-icloud-show',
    //                 text:'iCloud信息配置'
    //             },{
    //                 name:'system-app-config',
    //                 text:'代理商配置'
    //             }*/],
    //         }])
    //         return
    //     }
    //     if(value == 'icould'){
    //         newarr.push([{
    //             text:'iCloud账号管理',
    //             router:'icloud',
    //             opened:false,
    //             subRouters:[{
    //                 name:'icloud-index',
    //                 text:'iCloud账号管理'
    //             }],
    //         }])
    //         return
    //     }
    //     if(value == 'auth'){
    //         newarr.push([{
    //             text:'认证申请管理',
    //             router:'auth-apply',
    //             opened:false,
    //             subRouters:[/*{
    //                 name:'auth-apply-wait',
    //                 text:'待审核'
    //             },*/{
    //                 name:'auth-apply-history',
    //                 text:'历史审核'
    //             }],
    //         }])
    //         return
    //     }
    //     if(value == 'loan'){
    //         newarr.push([{
    //             text:'借款申请管理',
    //             router:'loan-apply',
    //             opened:false,
    //             subRouters:[{
    //                 name:'loan-apply-wait',
    //                 text:'待审核'
    //             },{
    //                 name:'loan-apply-history',
    //                 text:'历史审核'
    //             }/*,{
    //                 name:'finance-out',
    //                 query:{
    //                     type:'other'
    //                 },
    //                 text:'放款记录查询'
    //             }*/],
    //         }])
    //         return
    //     }
    //     if(value == 'lend'){
    //         newarr.push([{
    //             text:'财务管理',
    //             router:'finance',
    //             opened:false,
    //             subRouters:[{
    //                 name:'finance-wait',
    //                 text:'待放款记录'
    //             }/*,{
    //                 name:'finance-reject',
    //                 text:'拒绝放款记录'
    //             }*//*,{
    //                 name:'finance-ing',
    //                 text:'放款中记录'
    //             }*/,{
    //                 name:'finance-out',
    //                 text:'放款记录'
    //             },{
    //                 name:'receipt',
    //                 text:'应收账款统计'
    //             },{
    //                 name:'finance-extend',
    //                 text:'展期记录'
    //             }/*,{
    //                 name:'customer-management',
    //                 text:'客户管理'
    //             }*/],
    //         }])
    //         return
    //     }
    //     if(value == 'repay'){
    //         newarr.push([{
    //             text:'贷后管理',
    //             router:'after-loan',
    //             opened:false,
    //             subRouters:[{
    //                 name:'after-loan-wait',
    //                 text:'案件催收' //待催收
    //             },{
    //                 name:'after-loan-history',
    //                 text:'个人对账列表' //历史催收
    //             },/*{
    //                 name:'after-loan-error',
    //                 text:'异常催收'
    //             },*//*,{
    //                 name:'finance-out',
    //                 query:{
    //                     type:'other'
    //                 },
    //                 text:'放款记录查询'
    //             }*/{
    //                 name:'after-loan-allot-index',
    //                 query:{
    //                     routerType:'all'
    //                 },
    //                 text:'案件管理'
    //             }],
    //         }])
    //         return
    //     }
    //    /* if(value == 'repay_allot'){
    //         newarr.push([{
    //             text:'贷后数据看板', //贷后分配
    //             router:'after-loan-allot',
    //             opened:false,
    //             subRouters:[/*{
    //                 name:'after-loan-allot-index',
    //                 query:{
    //                     routerType:'all'
    //                 },
    //                 text:'案件管理'
    //             },{
    //                 name:'after-loan-wait',
    //                 query:{
    //                     routerType:'all'
    //                 },
    //                 text:'全部待催收'
    //             },
    //             {
    //                 name:'after-loan-history',
    //                 query:{
    //                     routerType:'all'
    //                 },
    //                 text:'全部历史催收'
    //             },{
    //                 name:'after-loan-error',
    //                 query:{
    //                     routerType:'all'
    //                 },
    //                 text:'全部异常催收'
    //             }
    //             ],
    //         }])
    //         return
    //     }*/
    //     if(value == 'operation'){
    //         newarr.push([{
    //             text:'运营管理',
    //             router:'operate',
    //             opened:false,
    //             subRouters:[/*{
    //                 name:'operate-user',
    //                 text:'运营人员管理'
    //             },{
    //                 name:'operate-auto-assign',
    //                 text:'自动分配管理'
    //             },*/{
    //                 name:'operate-assign-records',
    //                 text:'认证分配记录'
    //             },{
    //                 name:'operate-assign-records-loan',
    //                 text:'借款分配记录'
    //             }],
    //         }])
    //         return
    //     }
    // })
    commit(types.SET_MENULIST, valueArr)
    //console.log(newarr)
    Storage.set(`51SjdAdmin_menuList`,valueArr)
    Storage.set(`51SjdAdmin_menuArr`,arr)
}


// setSearchHistoryCurrentPage
export const setLoanIndexRefresh = ({ commit, state },boolean ) => {
    commit(types.SET_LOANINDEXREFRESH, boolean)
}

// setAppConfig
export const setAppConfig = ({ commit, state },object ) => {
    commit(types.SET_APPCONFIG, object)
}

// setSessionId
export const setChannel = ({ commit, state }, channel ) => {
	commit(types.SET_CHANNEL, channel)
    Storage.set('channel',channel)
}

// setAdmin
export const setAdmin = ({ commit, state }, admin ) => {
	commit(types.SET_ADMIN, admin)
    Storage.set('admin',admin)
}

// setSessionId
export const setSessionId = ({ commit, state }, sessionId ) => {
    let channel = state.localInfo.channel
    commit(types.SET_SESSIONID, sessionId)
    Storage.set(`51SjdAdmin_sessionId`,sessionId)
    $.extend($.ajaxSettings, {
        headers: {
            'channel': channel,
            'X-Requested-With':'XMLHttpRequest',
            'sessionId':sessionId || ''
        }
    })
}
export const setUserInfo = ({ commit, state }, name ) => {
    commit(types.SET_USERINFO, name)
    Storage.set(`51SjdAdmin_userinfo`,name)
}

export const setRouterList = ({ commit, state }, routerItem) => commit(types.SET_ROUTERLIST, routerItem)
export const setRouterAnimation = ({ commit, state }, animationName ) => commit(types.SET_ROUTERANIMATION, animationName)

export const setSelectZIndex = ({ commit, state }, num ) => commit(types.SET_SELECTZINDEX, num)

export const setDocumentClick = ({ commit, state }, boolean ) => commit(types.SET_DOCUMENTCLICK, boolean)

export const setNeedLogin = ({ commit, state }, boolean ) => commit(types.SET_NEEDLOGIN, boolean)

export const setCity = ({ commit, state }, cityInfo ) => commit(types.SET_CITY, cityInfo)
export const setCarInfo = ({ commit, state }, info ) => commit(types.SET_CARINFO, info)
export const setCarCurrent = ({ commit, state }, info ) => commit(types.SET_CARCURRENT, info)
export const setUserId = ({ commit, state }, id) => commit(types.SET_USERID, id)
export const setCardPrice = ({ commit, state }, priceArr) => commit(types.SET_CARDPRICE, priceArr)

export const addCard = ({ commit, state }, card) => commit(types.ADD_CARD, card)
export const editCard = ({ commit, state }, index, card) => commit(types.EDIT_CARD, index, card)
export const deleteCard = ({ commit, state }, card) => commit(types.DELETE_CARD, card)

export const setBaodanresult = ({ commit, state }, result) => commit(types.SET_BAODANRESULT, result)
export const removeBaodanresult = ({ commit, state }, result) => commit(types.REMOVE_BAODANRESULT, result)

export const setIndexViewForm = ({ commit, state }, form) => commit(types.SET_INDEXVIEWFORM, form)

export const setUpdateDashboard = ({ commit, state }, boolean ) => commit(types.SET_UPDATEDASHBOARD, boolean)
export const setDashboard = ({ commit, state }, dashboard ) => commit(types.SET_DASHBOARD, dashboard)

export const setRequestParams = ({ commit, state }, requestparams ) => commit(types.SET_REQUESTPARAMS, requestparams)
export const setAuthType = ({ commit, state }, type ) => commit(types.SET_AUTHTYPE, type)


